package com.tianhui.consignortwofei.mvp.ui.activity.hetong.activity;

import android.os.Bundle;

import com.fgs.common.CommonResponse;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;

import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.hetong.ContractInfo;
import com.tianhui.consignortwofei.mvp.model.hetong.ContractModel;
import com.tianhui.consignortwofei.mvp.ui.activity.hetong.HeTongShenPiActivity;

/**
 * @author fgs
 * @date 2021/5/19
 * @description
 **/
public abstract class BaseContractAddActivity extends BaseActivity {

    public ContractInfo mContractInfo;
    public ContractModel mContractModel;


    public abstract ContractInfo getSaveInfo();

    public abstract void initData();

    public abstract void initView();

    public abstract void initListener();

    @Override
    public void init() {
        setTitle("添加合同");
        mContractModel = new ContractModel();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            mContractInfo = bundle.getParcelable(KeyValue.KEY_CONTRACT_INFO);
//            LogUtil.print("传递的数据", JsonUtil.toJson(mContractInfo));
//            LogUtil.print("传递的数据===CompanyA",  mContractInfo.company_a);
//            LogUtil.print("传递的数据===contractId",  mContractInfo.id);
        }

        if (mContractInfo == null)
            mContractInfo = new ContractInfo();

        initData();
        initView();
        initListener();
    }

    public void requestContractCommit(ContractInfo contractInfo) {
        mContractModel.contractAddModel(getContext(), contractInfo, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    EventBusManage.refreshContractList();
//                    openActivity(HeTongActivityNew.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("contractno", contractInfo.contractno);
                    openActivity(HeTongShenPiActivity.class,bundle);

                } else {
                    showToast(commonResponse.getMsg());
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    protected void openNextStepActivity( ContractInfo contractInfo,Class pClass) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KeyValue.KEY_CONTRACT_INFO, contractInfo);
        openActivity(pClass, bundle);
    }





}
